package com.tfjybj.itoo.teach.provider.facade;

import org.apache.dubbo.config.annotation.Service;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.FacadeApi.StaffInstitutionApi;
import com.tfjybj.itoo.teach.entity.StaffInstitutionEntity;
import com.tfjybj.itoo.teach.model.StaffInstitutionModel;
import com.tfjybj.itoo.teach.provider.service.StaffInstitutionService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DUYU
 * @date 2019-5-22 16:06:37
 */
@Service
@org.springframework.stereotype.Service
public class StaffInstitutionFacade implements StaffInstitutionApi {

    @Resource
    private StaffInstitutionService staffInstitutionService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model StaffInstitutionModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult create(StaffInstitutionModel model) {
        if (StringUtils.isEmpty(model.getStaffId())) {
            return ItooResult.build(ItooResult.FAIL, "staffId为空");
        }
        if (StringUtils.isEmpty(model.getInstitutionId())) {
            return ItooResult.build(ItooResult.FAIL, "institutionId为空");
        }
        StaffInstitutionEntity staffInstitutionEntity = new StaffInstitutionEntity();
        BeanUtils.copyProperties(model, staffInstitutionEntity);
        staffInstitutionService.save(staffInstitutionEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult delete(String id) {
        staffInstitutionService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult deleteByIds(List<String> ids) {
        staffInstitutionService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model StaffInstitutionModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult modify(StaffInstitutionModel model) {
        if (StringUtils.isEmpty(model.getStaffId())) {
            return ItooResult.build(ItooResult.FAIL, "staffId为空");
        }
        if (StringUtils.isEmpty(model.getInstitutionId())) {
            return ItooResult.build(ItooResult.FAIL, "institutionId为空");
        }
        StaffInstitutionEntity staffInstitutionEntity = new StaffInstitutionEntity();
        BeanUtils.copyProperties(model, staffInstitutionEntity);
        staffInstitutionService.updateById(staffInstitutionEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找StaffInstitution
     *
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult findById(String id){
        StaffInstitutionEntity staffInstitutionEntity = staffInstitutionService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffInstitutionEntity);
    }

    /**
     * 分页查询所有StaffInstitution
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult queryPageAll(Integer pageNo, Integer pageSize) {
        PageInfo<StaffInstitutionEntity> staffInstitutions = staffInstitutionService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffInstitutions);
    }

    /**
     * 根据职工id模糊查询staffInstitution
     *
     * @param staffId  职工id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的staffInstitution
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @Override
    public ItooResult queryStaffInstitutionByLikeStaffId(String staffId, Integer pageNo, Integer pageSize) {
        PageInfo<StaffInstitutionEntity> staffInstitutionList = staffInstitutionService.queryByLikeStaffId(staffId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffInstitutionList);
    }
    //endregion


    /**
     * 根据教师id查询所属机构
     *
     * @since 2018-12-9 12:00:34
     * @author maying
     * @param userId userId
     * @return List<StaffInstitutionModel>
     */
    @Override
    public ItooResult queryCollege(String userId){
        if (StringUtils.isEmpty(userId)){
            return ItooResult.build(ItooResult.FAIL,"userid不能为空");
        }
        return ItooResult.build(ItooResult.SUCCESS,"查询所属机构成功",staffInstitutionService.queryCollege(userId));
    }
}
